package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author Justin Parker
 * @Date 2024/8/9 0009 11:23
 * @Version 1.0
 */
@Api(tags = "03.评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation(value = "添加评论功能")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
    UserVo  userVo = (UserVo)session.getAttribute("user");

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVo.getId());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }

    @ApiOperation(value = "根据微博id查询评论功能")
    @GetMapping("/selectByWeiboId")
    public JsonResult selectByWeiboId(Integer id) {
        System.out.println("id = " + id);
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVOS);
    }
}
